1519C - Berland Regional - CodeForces Solution


brute force data structures greedy number theory sortings *1400

Please click on ads to support us..

Python Code:

def rt(n,r,m):
    dp=[[] for i in range(0,m)]
    for i in range(0,len(n)):
        dp[n[i]-1].append(r[i])
    q=[]
    p1=[[0 for i in range(0,len(dp[k]))] for k in range(0,m)]
    for i in range(0,len(dp)):
        dp[i].sort(reverse=True)
        for x in range(0,len(dp[i])):
            if(x==0):
                p1[i][x]=dp[i][x]
            else:
                p1[i][x]=p1[i][x-1]+dp[i][x]
        if(len(dp[i])>=1):
            q.append([len(dp[i]),i])
    s=0
    q.sort()
    l1=1
    r1=[0 for i in range(0,m)]

    while(s<len(q)):
        c1=0
        for k in range(s,len(q)):
            [a,b]=q[k]
            if(a>=l1):
                z1=(a-a%l1-1)
                c1=c1+p1[b][z1]
            else:
                s=s+1
        if(l1-1<m):
           r1[l1-1]=c1

        l1=l1+1

    return r1
s=int(input())
for i in range(0,s):
    s1=int(input())
    n=list(map(int,input().split()))
    r=list(map(int,input().split()))
    x=rt(n,r,s1)
    for j in x:
        print(j,end=" ")
    print("")

C++ Code:

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)

using namespace std;


int main() {
    int t;
    scanf("%d", &t);
    forn(_, t){
        int n;
        scanf("%d", &n);
        vector<int> s(n), u(n);
        forn(i, n){
            scanf("%d", &s[i]);
            --s[i];
        }
        forn(i, n){
            scanf("%d", &u[i]);
        }
        vector<vector<int>> bst(n);
        forn(i, n) bst[s[i]].push_back(u[i]);
        forn(i, n) sort(bst[i].begin(), bst[i].end(), greater<int>());
        vector<vector<long long>> pr(n, vector<long long>(1, 0));
        forn(i, n) for (int x : bst[i]) pr[i].push_back(pr[i].back() + x);
        vector<long long> ans(n);
        forn(i, n) for (int k = 1; k <= int(bst[i].size()); ++k)
            ans[k - 1] += pr[i][bst[i].size() / k * k];
        forn(i, n)
            printf("%lld ", ans[i]);
        puts("");
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes